package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.NetworkDriver;
import com.bocloud.cmp.driver.providers.ali.AliNetworkDriver;
import com.bocloud.cmp.driver.providers.intf.NetworkProvider;
import com.bocloud.cmp.driver.providers.jd.JdNetworkDriver;
import com.bocloud.cmp.driver.providers.os.OSNetworkDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.providers.vmware.VmwareNetworkDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.NetworkModel;
import com.bocloud.cmp.model.SwitchModel;
import com.bocloud.common.model.BsmResult;

/**
 * 网络操作服务接口实现类
 * 
 * @author weiwei
 *
 */
@Service("networkProvider")
public class NetworkProviderImpl implements NetworkProvider {

	@Override
	public BsmResult list(String vendor, String region) {
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			break;
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.list();
		}
	}

	@Override
	public BsmResult create(String vendor, String region, NetworkModel model) {
		BsmResult result = null;
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.create(model);
			break;
		case ALIYUN:
			driver = new AliNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.create(model);
			break;
		case OPENSTACK:
			model.setTenant(butler.getTenant());
			driver = new OSNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			result = driver.create(model);
			break;
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			result = driver.create(model);
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String networkId, String vendor, String region) {
		BsmResult result = null;
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.remove(networkId);
			break;
		case ALIYUN:
			driver = new AliNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.remove(networkId);
			break;
		case OPENSTACK:
			driver = new OSNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			result = driver.remove(networkId);
			break;
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			result = driver.remove(networkId);
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String networkId, String vendor, String region) {
		BsmResult result = null;
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.detail(networkId);
			break;
		case ALIYUN:
			driver = new AliNetworkDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.detail(networkId);
			break;
		case OPENSTACK:
			driver = new OSNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			result = driver.detail(networkId);
			break;
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			result = driver.detail(networkId);
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult modify(String vendor, String region, NetworkModel model) {
		BsmResult result = null;
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case OPENSTACK:
			driver = new OSNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			result = driver.modify(model);
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

	@Override
	public BsmResult createDSwitch(String vendor, SwitchModel switchModel) {
		BsmResult result = null;
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case OPENSTACK:
			break;
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			result = driver.createDSwitch(switchModel);
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORT");
			break;
		}
		return result;
	}

	@Override
	public BsmResult createVSwitch(String vendor, SwitchModel switchModel) {
		NetworkDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case VMWARE:
			driver = new VmwareNetworkDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.createVSwitch(switchModel);
		}
	}

}
